﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;

public partial class UserLogin : System.Web.UI.Page
{
    Operation operation = new Operation();　//声明业务类
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LoginButton_Click(object sender, EventArgs e)
    {
        String Email = this.UserName.Text.Trim();
        String Password = this.Password.Text.Trim();
        DataSet ds;// = operation.LogonWithEmail(Email, Password);
        if (Email.IndexOf("@") != -1)
            ds = operation.ValidateEmail(Email);
        else
            ds = operation.ValidateUser(Email);
        this.FailureText.Text = "对不起，登陆失败";
        this.FailureText.Visible = true;
        if (ds.Tables["tb_user"].Rows.Count > 0)  //判断用户是否合法。
        {
            if (ds.Tables[0].Rows[0][2].ToString().CompareTo(Password) == 0)
            {
                bool isActive = (bool)(ds.Tables["tb_user"].Rows[0][9]);
                if (isActive)
                {
                    string username = ds.Tables["tb_user"].Rows[0][1].ToString();
                    Session.Add("username", username);  //将用户名称存储在Session中,用于判断用户是否登录。
                    Session.Add("Email", Email);
                    Session.Add("userid", ds.Tables["tb_user"].Rows[0][0].ToString());
                    this.FailureText.Visible = false;
                    Response.Redirect("~/Default.aspx");
                   // welcome.Text = "你好，" + username;
                   // welcome.Visible = true;
                   // loginOut.Visible = true;
                   // UserManager.Visible = true;
                }
                else
                {
                    Response.Write("<script language=javascript>alert('你未激活，请先激活！');</script>");
                }
            }
            else
            {
                Response.Write("<script language=javascript>alert('密码不正确！');</script>");
            }
        }
        else
        {
            Response.Write("<script language=javascript>alert('账户不存在！');</script>");
        }
    }
}
